﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using Common.Utils;
using Common.ViewModels;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using S7.Net.Types;
using Double = S7.Net.Types.Double;

namespace PlatingLineUI
{
    public class TestVM : ReactiveObject
    {
        [Reactive] public double Angle { get; set; }

        [Reactive] public LineRigidBody LineRigidBody { get; set; }

        public void Test()
        {
            Task.Run(() =>
            {
                for (int i = 0; i <= 90; i += 2)
                {
                    Angle = i;
                    Thread.Sleep(50);
                }
            });
        }

        /// <summary>
        /// 这里调度按照调度周期来算
        /// </summary>
        public void Add()
        {
            //LineRigidBody lineRigidBody
            double 第一层Y值 = 2100;
            double 起点X值 = 1200;
            double 水平面Z值 = 800;
            double 轨道面Z值 = 1200;
            List<节点> 内部节点s = new List<节点>();

            节点 冷拖入口节点1 = new 节点() { Name = "冷拖入口节点1", ArrayIndex = 0 };
            冷拖入口节点1.Point3D.X = 起点X值 - 300;
            冷拖入口节点1.Point3D.Y = 第一层Y值;
            内部节点s.Add(冷拖入口节点1);
            节点 冷拖入口节点2 = new 节点() { Name = "冷拖入口节点2", ArrayIndex = 1 };
            冷拖入口节点2.Point3D.X = 起点X值 - 300;
            冷拖入口节点2.Point3D.Y = 第一层Y值;
            内部节点s.Add(冷拖入口节点2);

            节点 冷拖连通节点 = new 节点() { Name = "冷拖连通节点" };

            for (int i = 1; i <= 7; i++)
            {
                节点 item节点 = new 节点() { Name = $"第一排工位节点{i}", ArrayIndex = i + 1 };
                item节点.Point3D.Y = 第一层Y值;
                item节点.Point3D.X = 起点X值 + 350 + 700 * (i - 1);
                item节点.Point3D.Z = 水平面Z值;
                内部节点s.Add(item节点);
            }

            //根据机械手 直接调度
            边[,] 内部存储二维矩阵 = new 边[9, 9];
            for (int i = 0; i < 9; i++)
            {
                节点 节点i = 内部节点s[i];
                for (int j = 0; j < 9; j++)
                {
                    边 item = new 边();
                    内部存储二维矩阵[i, j] = item;
                    if (j <= 1)
                    {
                        item.存在边 = false;
                        continue;
                    }

                    if (i == j)
                    {
                        item.存在边 = false;
                        continue;
                    }
                    节点 节点j = 内部节点s[j];

                    //内部存储二维矩阵.
                    item.Time = Get(节点i.Point3D, 节点j.Point3D);
                }
            }

            int[,] vInts = new int[9, 9];
        }

        /// <summary>
        /// 寻找最优机械手
        /// </summary>
        public void Serch最近()
        {

        }

        public static double Get(Point3D point3D1, Point3D point3D2)
        {
            double ret = 0;
            double distance = Math.Abs(point3D1.X - point3D2.X);
            if (distance == 0)
            {
                return 0;
            }
            ret = ProductLine.Instance.摆渡加速时间 + distance / ProductLine.Instance.BoatSpeed;
            return ret;
        }

        public void 添加很多工位类型()
        {
            Add("冷脱1", 3);
            Add("冷脱2", 3);

            Add("冷脱水洗1", 0);//冷脱水洗
            Add("冷脱水洗2", 0);//冷脱水洗
            Add("冷脱水洗3", 0);//冷脱水洗

            Add("超声波1", 8);
            Add("超声波2", 8);
            Add("超声波水洗1", 0);
            Add("超声波水洗2", 0);

            Add("阳极电解", 1);
            Add("阴极电解", 2);

            Add("电解水洗1", 0);
            Add("电解水洗2", 0);
            Add("电解水洗3", 0);

            Add("活化1", 1);

            Add("活化水洗1", 0);
            Add("活化水洗2", 0);
            Add("活化水洗3", 0);
            Add("碱铜", 8);
            Add("碱铜水洗1", 0);
            Add("碱铜水洗2", 0);
            Add("碱铜水洗3", 0);

            Add("活化2", 1);
            Add("活化2水洗1", 0);
            Add("活化2水洗2", 0);
            Add("活化2水洗3", 0);
            Add("高酸低铜", 8);
            Add("低酸高铜", 8);
        }

        /// <summary>
        /// 这里对于水洗和活化,采用向下查找的办法
        /// </summary>
        public void 添加很多工位类型2()
        {
            
            Add("无",0);//没有工位类型 用于上料位或者下料位()
            Add("冷脱1", 3);
            Add("冷脱2", 3);

            //多级水洗
            for (int i = 0; i < 5; i++)
            {
                AddSencond($"片区{i+1}一级水洗", 5);
                AddSencond($"片区{i + 1}二级水洗", 5);
                AddSencond($"片区{i + 1}三级水洗", 5);
            }
            


            Add("超声波1", 8);
            Add("超声波2", 8);

            Add("阳极电解", 1);
            Add("阴极电解", 2);


            Add("活化", 1);

            Add("碱铜", 8);

            Add("高酸低铜", 8);
            Add("低酸高铜", 10);

            Add("光镍", 20);
            Add("半光镍", 20);

            Add("镀金", 30);

            Add("中和", 1);

            Add("保护", 1);


            Add("预镀银", 1);

            Add("镀银", 40);



            Add("镀锡", 40);



        }

        /// <summary>
        /// 
        /// </summary>
        public void 添加主通道()
        {
            //Task.Run(() =>
            //{
                添加很多工位类型2();//添加很多工位类型
                for (int i = 0; i <= 4; i++)
                {
                    Add通道(i);
                    //return;
                }

                MessageAggregator<LoadedMsg>.Publish(new LoadedMsg());
                for (int i = 0; i < 3; i++)
                {
                    var passVM = ProductLine.Instance.PassVMs[i];
                    var passVM后 = ProductLine.Instance.PassVMs[i + 1];
                    var distance = passVM后.LineRigidBody.PointStart.Y -
                                   (passVM.LineRigidBody.PointStart.Y + passVM.LineRigidBody.YLength);
                }



                foreach (var passVM in ProductLine.Instance.PassVMs)
                {

                }
            //});
           
        }

        private void Add通道(int i)
        {
            double 初始Y值 = 1000;
            double 初始X值 = 1000;
            double 摆渡间隙 = 100;
            double 轨道直径 = 600;
            double 上下料长度 = 700;
            double 池子直径 = 700;
            double 摆渡直径 = 800;
            double 小池子间隔 = 10;
            double 大类别间隔 = 100;
            double 区域整体长度 = (池子直径 * 2 + 小池子间隔) * 2 + 摆渡直径 + 大类别间隔;

            if (i == 0)
            {
                添加上通道();
                //return;
            }
            添加下通道();

            void 添加上通道()
            {
                PassVM passVM上 = new PassVM() { Name = $"通道{i + 1}上" };
                passVM上.StartPossition = 0;
                passVM上.Length = 上下料长度 + 池子直径 * 2 + 小池子间隔;
                //直线段
                passVM上.LineRigidBody = new LineRigidBody()
                {
                    Name = $"通道{i + 1}上",
                    Zindex = 0,
                };
                passVM上.LineRigidBody.PointStartBase.X = 初始X值 + 轨道直径 * 2 + 摆渡间隙 * 2 + 轨道直径 / 2;
                if (i == 0)
                {
                    passVM上.LineRigidBody.PointStartBase.Y = 初始Y值;
                }
                else
                {
                    passVM上.LineRigidBody.PointStartBase.Y = 上下料长度 + 区域整体长度 * i;
                }

                //passVM上.LineRigidBody.Background = Brushes.Chartreuse;

                passVM上.LineRigidBody.XLength = 轨道直径;
                passVM上.LineRigidBody.YLength = 上下料长度 + (池子直径 * 2 + 小池子间隔);
                passVM上.LineRigidBody.ZLength = 1000;//高于地平面
                passVM上.LineRigidBody.IsUseXOffset = true;
                ProductLine.Instance.PassVMs.Add(passVM上);
                this.LineRigidBody = passVM上.LineRigidBody;

                ///添加上料位
                Station station上料位 = new Station()
                {
                    Name = "上料位",
                };
                station上料位.PassVMPosition = new PassVMPosition()
                {
                    PassVM = passVM上,
                    Position = 100,
                };
                station上料位.PassVMPositions.Add(station上料位.PassVMPosition);
                ProductLine.Instance.Stations.Add(station上料位);

                Station station下料位 = new Station() { Name = "下料位" };
                station下料位.PassVMPosition = new PassVMPosition()
                {
                    PassVM = passVM上,
                    Position = 600,
                };
                station下料位.PassVMPositions.Add(station上料位.PassVMPosition);
                ProductLine.Instance.Stations.Add(station下料位);
                Fill竖直系数(passVM上);
            }

            void 添加下通道()
            {
                PassVM passVM下 = new PassVM() { Name = $"通道{i + 1}下" };
                //直线段
                passVM下.LineRigidBody = new LineRigidBody()
                {
                    Name = $"通道{i + 1}下",
                    Zindex = 0,
                    FontSize=100,
                    TypeFaceIsVisiable=false,
                };
                passVM下.LineRigidBody.PointStartBase.X = 初始X值 + 轨道直径 * 2 + 摆渡间隙 * 2 + 轨道直径 / 2;//X值
                passVM下.LineRigidBody.PointStartBase.Y = 初始Y值 + 上下料长度 + 池子直径 * 2 + 小池子间隔 + 摆渡直径 + (区域整体长度) * i;//Y值
                passVM下.LineRigidBody.XLength = 轨道直径;
                passVM下.LineRigidBody.YLength = (池子直径 * 2 + 小池子间隔) * 2 + 大类别间隔;
                passVM下.LineRigidBody.ZLength = 1000;//高于地平面
                passVM下.StartPossition = passVM下.LineRigidBody.PointStartBase.Y;

                passVM下.Length = passVM下.LineRigidBody.YLength;

                passVM下.LineRigidBody.IsUseXOffset = true;
                ProductLine.Instance.PassVMs.Add(passVM下);
                //添加一些站点
                //Station station = new Station() { };
                Fill竖直系数(passVM下);
                添加摆渡通道(passVM下);

                //添加两个站点
                Station station上方 = new Station()
                {
                    Name = $"入站{i+1}",
                };
                station上方.PassVMPosition = new PassVMPosition()
                {
                    PassVM = passVM下,
                    Position = passVM下.StartPossition + passVM下.LineRigidBody.YLength/2,
                };


                //将站点关系载入
                station上方.PassVMPositions.Add(station上方.PassVMPosition);

                ProductLine.Instance.Stations.Add(station上方);
            }

            ///添加摆渡通道 
            void 添加摆渡通道(PassVM passVM下)
            {
                PassVM passVM = new PassVM() { Name = $"摆渡通道{i + 1}" };
                passVM.LineRigidBody = new()
                {
                    Name = $"摆渡通道{i + 1}",
                    Zindex = 0,
                };
                passVM.LineRigidBody.XLength = 轨道直径 * 2 + 摆渡间隙 * 2 + 轨道直径 + (池子直径 + 小池子间隔) * 7;
                passVM.池子起点 = 初始X值 + 轨道直径 * 2 + 摆渡间隙 * 2 + 轨道直径;
                passVM.LineRigidBody.YLength = 摆渡直径;
                passVM.LineRigidBody.ZLength = passVM下.LineRigidBody.ZLength;//Z轴长度

                passVM.StartPossition = 0;
                passVM.Length = passVM.LineRigidBody.XLength;
                passVM.LineRigidBody.IsUseYOffset = true;//Y方向


                passVM.LineRigidBody.PointStartBase.X = 初始X值;//起点X为0
                passVM.LineRigidBody.PointStartBase.Y = passVM下.LineRigidBody.PointStartBase.Y - 摆渡直径 / 2;
                passVM.LineRigidBody.Background = (SolidColorBrush)new BrushConverter().ConvertFromString("#FF3d7567");
                ProductLine.Instance.PassVMs.Add(passVM);
                Fill水平系数(passVM);
                添加摆渡(passVM,passVM下);
                添加池子(passVM);
               
            }

            //添加摆渡和摆渡站点
            void 添加摆渡(PassVM passVm摆渡通道,PassVM passVM下)
            {
                //添加三个摆渡
                for (int j = 1; j <= 3; j++)
                {
                    BoatVM boatVm1 = new BoatVM() { Name = $"摆渡{i + 1}_{j}" };
                    boatVm1.LineRigidBody = new() { Name = $"摆渡{i + 1}_{j}", Zindex = 1 };

                    //这里可以没有父节点
                    boatVm1.LineRigidBody.Parent = passVm摆渡通道.LineRigidBody;//摆渡属于通道 此时摆渡仅仅剩下一个位置。
                    boatVm1.LineRigidBody.PassVM = passVm摆渡通道;
                    boatVm1.PassVM= passVm摆渡通道;

                    Fill竖直系数(boatVm1);
                    //X Y Z方向
                    boatVm1.LineRigidBody.XLength = 轨道直径;
                    boatVm1.LineRigidBody.YLength = 摆渡直径;
                    boatVm1.LineRigidBody.ZLength = 1000;//高于地平面
                    boatVm1.LineRigidBody.IsUseXOffset = true;
                    boatVm1.LineRigidBody.IsUseYOffset = true;
                    boatVm1.LineRigidBody.Background = (SolidColorBrush)new BrushConverter().ConvertFromString("#EFDFC57B");

                    boatVm1.StartPossition = passVm摆渡通道.LineRigidBody.PointStartBase.Y- passVm摆渡通道.LineRigidBody.YLength/2;
                    //依附于通道的摆渡不需要起点
                    boatVm1.LineRigidBody.Position = 轨道直径/2 + (轨道直径+ 摆渡间隙) * (j - 1);
                    ProductLine.Instance.PassVMs.Add(boatVm1);

                  
                }


                Station station最左边=new Station()
                {
                    Name = $"左边{i + 1}",
                };
                station最左边.PassVMPosition = new PassVMPosition()
                {
                    PassVM = passVm摆渡通道,
                    Position = passVm摆渡通道.StartPossition  + 轨道直径 / 2,
                };
                station最左边.PassVMPositions.Add(station最左边.PassVMPosition);
                ProductLine.Instance.Stations.Add(station最左边);
                passVm摆渡通道.Stations.Add(station最左边);

                Station station次左边 = new Station()
                {
                    Name = $"次左边{i + 1}",
                };

                station次左边.PassVMPosition = new PassVMPosition()
                {
                    PassVM = passVm摆渡通道,
                    Position = passVm摆渡通道.StartPossition+ 轨道直径+ 摆渡间隙 + 轨道直径 / 2,
                };
                station次左边.PassVMPositions.Add(station次左边.PassVMPosition);
                ProductLine.Instance.Stations.Add(station次左边);
                passVm摆渡通道.Stations.Add(station次左边);

                //三个摆渡都可以到达此站点
                Station 摆渡中间 = new Station()
                {
                    Name = $"摆渡中间{i + 1}",
                };
                //只有世界坐标系有用。
                var passVMPosition = new PassVMPosition()
                {
                    PassVM = passVM下,
                    Position = passVM下.StartPossition - 摆渡直径 / 2,
                };
                //添加摆渡在这个站点的点位说明()
                摆渡中间.PassVMPositions.Add(passVMPosition);
                //这里还需要添加一个坐标系           垂直方向坐标系
                摆渡中间.PassVMPosition = new PassVMPosition()
                {
                    PassVM = passVm摆渡通道,
                    Position = passVm摆渡通道.StartPossition+ 轨道直径 * 2 + 摆渡间隙 * 2+轨道直径/2,
                };
                //添加摆渡在这个站点的点位说明
                摆渡中间.PassVMPositions.Add(摆渡中间.PassVMPosition);
                ProductLine.Instance.Stations.Add(摆渡中间);
                passVm摆渡通道.Stations.Add(摆渡中间);

                //这里需要添加自身的摆渡站点
            }

            //池子是不能动的
            void 添加池子(PassVM passVm摆渡通道)
            {

                //添加一些站点 为站点设置临接链表。 一个站点世界坐标系位置确定，但是可能在不同轨道代表不同位置
                //for (int j = 0; j < 7; j++)
                //{
                //    Station Station = new Station();

                //}

                //为工位分配站点
                for (int j = 1; j <= 28; j++)
                {
                    池子 池子 = new 池子()
                    {
                        Name = $"冷脱{j}"
                    };
                    池子.PassVM= passVm摆渡通道;
                    池子.所属通道 = passVm摆渡通道;
                    池子.Background = (SolidColorBrush)new BrushConverter().ConvertFromString("#FF7598b4");
                    池子.Fore =Brushes.White;
                    池子.XLength = 池子直径;
                    池子.YLength = 池子直径;
                    池子.IsUseXOffset=true;
                    池子.IsUseYOffset = true;
                    池子.Position = j;
                    池子.FontSize = 200;
                    ProductLine.Instance.池子s.Add(池子);
                    //WorkStation workStation = new WorkStation()
                    //{

                    //};
                    //workStation.Station
                    //workStation.
                    池子.Create();
                    //池子.Works.Add(workStation);
                    //添加工件工位
                   
                }

                //  
                double 首个池子位置 = passVm摆渡通道.StartPossition + 轨道直径 * 3 + 摆渡间隙 * 2 + 池子直径 / 2;
                for (int k = 0; k < 7; k++)
                {
                    Station station池子 = new Station()
                    {
                        Name = $"池子{i + 1}_{k + 1}",
                    };
                    station池子.PassVMPosition = new PassVMPosition()
                    {
                        PassVM = passVm摆渡通道,
                        Position = 首个池子位置 + k*(池子直径+ 小池子间隔),
                    };
                    station池子.PassVMPositions.Add(station池子.PassVMPosition);
                    ProductLine.Instance.Stations.Add(station池子);
                    passVm摆渡通道.Stations.Add(station池子);
                }

                //这里要为池子配置工位,为摆渡配置工位

            }
        }

        public void Fill竖直系数(PassVM passVm)
        {
            passVm.TransformRatio.RatioX = 0;
            passVm.TransformRatio.RatioY = 1;
            passVm.TransformRatio.RatioZ = 0;
        }

        public void Fill水平系数(PassVM passVm)
        {
            passVm.TransformRatio.RatioX = 1;
            passVm.TransformRatio.RatioY = 0;
            passVm.TransformRatio.RatioZ = 0;
        }

        //void 添加下通道()
        //{
        //    PassVM passVM下 = new PassVM() { Name = $"通道{i}下" };
        //    //直线段
        //    passVM下.LineRigidBody = new LineRigidBody()
        //    {

        //    };
        //    passVM下.LineRigidBody.PointStartBase.X = 轨道直径 * 2 + 摆渡间隙 * 2 + 轨道直径 / 2;
        //    passVM下.LineRigidBody.PointStartBase.Y = 上下料长度 + (池子直径 * 2 + 小池子间隔 + 摆渡直径 + 大类别间隔) * i;

        //    ProductLine.Instance.PassVMs.Add(passVM下);
        //}


        /// <summary>
        /// 添加主通道2
        /// </summary>
        public void 添加主通道2()
        {
            //左边主通道 右边摆渡 通道片区方案
            添加很多工位类型2();

            //一条主通道  摆渡选配片区  按照片区进行调度 片区站点 选配1-2个机械手。当选配两个机械手时，每个负责一段片区 
            //划定片区之后 按照片区进行调度。每个(摆渡)指定片区。
            //for (int i = 0; i < 6; i++)
            //{
            //    Car car=new Car();
            //    car.Name = $"M{i + 1}";
            //    ProductLine.Instance.Cars.Add(car);
            //}

            double 初始Y值 = 1000;
            double 初始X值 = 1000;
            double 摆渡间隙 = 100;
            double 主轨直径 = 600;
            double 上下料长度 = 700;
            double 池子直径 = 700;
            double 工作轨道直径 = 800;
            double 小池子间隔 = 10;
            double 大类别间隔 = 100;
            double 区域整体长度 = (池子直径 * 2 + 小池子间隔) * 2 + 工作轨道直径 + 大类别间隔;

            PassVM passVMMain = new PassVM() { Name = $"主通道" };
            passVMMain.StartPossition = 0;
            passVMMain.Length = 上下料长度 + 池子直径 * 2 + 小池子间隔;
            //直线段
            passVMMain.LineRigidBody = new LineRigidBody()
            {
                Name = $"主通道",
                Zindex = 0,
            };
            ProductLine.Instance.主通道调度.PassVM = passVMMain;//为主通道设定
            //passVMMain.LineRigidBody.PointStartBase.X = 初始X值 + 轨道直径 * 2 + 摆渡间隙 * 2 + 轨道直径 / 2;
            passVMMain.LineRigidBody.PointStartBase.X = 初始X值 + 主轨直径 / 2;
            passVMMain.LineRigidBody.PointStartBase.Y = 初始Y值;

            passVMMain.LineRigidBody.XLength = 主轨直径;
            passVMMain.LineRigidBody.YLength = 上下料长度 + (池子直径 * 2 + 小池子间隔+工作轨道直径)*7;
            passVMMain.LineRigidBody.ZLength = 1000;//高于地平面
            passVMMain.LineRigidBody.IsUseXOffset = true;
            ProductLine.Instance.PassVMs.Add(passVMMain);
            this.LineRigidBody = passVMMain.LineRigidBody;

            ///添加上料位
            Station station上料位 = new Station()
            {
                Name = "上料位",
            };


            station上料位.PassVMPosition = new PassVMPosition()
            {
                PassVM = passVMMain,
                Position = 100,
            };
            station上料位.PassVMPositions.Add(station上料位.PassVMPosition);
            ProductLine.Instance.Stations.Add(station上料位);
            ProductLine.Instance.主通道调度.上料WorkStation.Station = station上料位;

            Station station下料位 = new Station() { Name = "下料位" };
            station下料位.PassVMPosition = new PassVMPosition()
            {
                PassVM = passVMMain,
                Position = 600,
            };
            station下料位.PassVMPositions.Add(station上料位.PassVMPosition);
            ProductLine.Instance.Stations.Add(station下料位);
            ProductLine.Instance.主通道调度.上料WorkStation.Station = station下料位;
            Fill竖直系数(passVMMain);

            //添加上下料工位


            var 主通道调度 = ProductLine.Instance.主通道调度;
            主通道调度.Car = new Car() { Name = $"M1" };
            ProductLine.Instance.Cars.Add(主通道调度.Car);
            主通道调度.Car.PassVM = 主通道调度.PassVM;
            主通道调度.Car.Position = 500;
            主通道调度.Car.Position = 500;
            主通道调度.Car.XLength = 600;
            主通道调度.Car.YLength = 600;
            主通道调度.Car.ZLength = 600;
            主通道调度.Car.Parent = 主通道调度.PassVM.LineRigidBody;
            主通道调度.Car.Background= Brushes.Blue;
            主通道调度.Car.IsUseXOffset = true;
            主通道调度.Car.IsUseYOffset=false;
            主通道调度.Car.FontSize = 140;

            //这里需要添加一些水平通道。小车在水平通道上进行运动
            //片区
            for (int i = 0; i < 4; i++)
            {
                Fill水平(i);
            }

            void Fill水平(int i)
            {
                片区 片区 = new 片区() { };
                ProductLine.Instance.片区s.Add(片区);

                PassVM passVM = new PassVM() { Name = $"工作通道{i + 1}" };
                passVM.LineRigidBody = new()
                {
                    Name = $"工作通道{i + 1}",
                    Zindex = 1,
                };
                passVM.LineRigidBody.XLength =  (池子直径 + 小池子间隔) * 7;
                passVM.池子起点 = 初始X值 + 主轨直径;
                passVM.LineRigidBody.YLength = 工作轨道直径;
                passVM.LineRigidBody.ZLength = 1000;//Z轴长度

                passVM.StartPossition = 0;
                passVM.Length = passVM.LineRigidBody.XLength;
                passVM.LineRigidBody.IsUseYOffset = true;//Y方向


                passVM.LineRigidBody.PointStartBase.X = 初始X值+主轨直径;//起点X为0
                passVM.LineRigidBody.PointStartBase.Y = 初始Y值 + 上下料长度 + 池子直径 * 2 + 小池子间隔 +  (区域整体长度) * i+工作轨道直径 /2;
                passVM.LineRigidBody.Background = (SolidColorBrush)new BrushConverter().ConvertFromString("#FF3d7567");
                ProductLine.Instance.PassVMs.Add(passVM);
                Fill水平系数(passVM);
                添加池子(片区, passVM,i);
                //为通道添加小车
                //这里引入片区
                
                片区.PassVM=passVM;
                片区.Stations = passVM.Stations;
                片区.CarCount = 1;
                if (i == 0)
                {
                    片区.CarCount = 2;
                }
                //片区.Cars.Add();
            }

            void 添加池子(片区 片区,PassVM passVm工作通道,int i)
            {

                double 首个池子位置 = passVm工作通道.StartPossition + 池子直径 / 2;
                for (int k = 0; k < 7; k++)
                {
                    Station station池子 = new Station()
                    {
                        Name = $"池子{i + 1}_{k + 1}",
                    };
                    station池子.PassVMPosition = new PassVMPosition()
                    {
                        PassVM = passVm工作通道,
                        Position = 首个池子位置 + k * (池子直径 + 小池子间隔),
                    };
                    var point3D = station池子.PassVMPosition.PassVM.GetPoint3D(station池子.PassVMPosition, true);
                    station池子.Point3D.Update(point3D);
                    station池子.PassVMPositions.Add(station池子.PassVMPosition);
                    ProductLine.Instance.Stations.Add(station池子);
                    passVm工作通道.Stations.Add(station池子);
                    片区.Stations.Add(station池子);
                }

                //为工位分配站点
                for (int j = 1; j <= 28; j++)
                {
                    池子 池子 = new 池子()
                    {
                        Name = $"冷脱{j}"
                    };
                    池子.PassVM = passVm工作通道;
                    池子.所属通道 = passVm工作通道;
                    池子.Background = (SolidColorBrush)new BrushConverter().ConvertFromString("#FF7598b4");
                    池子.Fore = Brushes.White;
                    池子.XLength = 池子直径;
                    池子.YLength = 池子直径;
                    池子.IsUseXOffset = true;
                    池子.IsUseYOffset = true;
                    池子.Position = j;
                    池子.FontSize = 200;
                    ProductLine.Instance.池子s.Add(池子);
                    池子.Create(); 
                    片区.池子s.Add(池子);

                    //为工位分配工作站点
                    foreach (WorkStation workStation in 池子.Works)
                    {
                        //确定工位的池子,追溯源头时使用
                        workStation.池子 = 池子;
                        片区.WorkStations.Add(workStation);
                        foreach (var station in 片区.Stations)
                        {
                            if (池子.PointStartBase.X == station.Point3D.X)
                            {
                                workStation.Station = station;
                                break;
                            }
                        }
                    }
                }

                //  
               

                //这里要为池子配置工位,


            }

            MessageAggregator<LoadedMsg>.Publish(new LoadedMsg());
           
        }

        


        public void 保存()
        {
            ProductLine.Instance.Save();
        }

        public void 显示()
        {
            ReflexBuildControl.AddWindow(ProductLine.Instance);

        }

        /// <summary>
        /// 一些简单的东西调度起来
        /// </summary>
        public void 单片区调度()
        {
            var 主通道调度 = ProductLine.Instance.主通道调度;
            //主通道调度.Car.工件 = new 工件() { Num = 1 };
            //主通道调度.Car.Have工件 = true;
            //主通道调度.上料WorkStation.Station = ProductLine.Instance.Stations[0];
            //主通道调度.下料WorkStation.Station= ProductLine.Instance.Stations[1];
            //foreach (var pq in ProductLine.Instance.片区s)
            //{
            //    pq.公共Station = pq.Stations[3];
            //    pq.Car2FreePos = 3200;
            //}

            //for (int i = 3; i < 4; i++)
            //{
            //    var pq = ProductLine.Instance.片区s[i];
                //pq.Car1 = new Car() { Name = "M7" };
                //pq.Car1.PassVM = pq.PassVM;
                //pq.Car1.Parent = pq.PassVM.LineRigidBody;
                //FillCar(pq.Car1);
                //pq.Car1.Position = 1700;
                //foreach (池子 pq池子 in pq.池子s)
                //{
                //    if (pq池子.工位类型 == null)
                //    {
                //        pq池子.Name = "备用";
                //        pq池子.工位类型 = ProductLine.Instance.工位类型s[0];
                //        pq池子.池子类型 = 池子类型.None;
                //    }
                //}
            //}


            //foreach (var pq in ProductLine.Instance.片区s)
            //{
            //    foreach (var pq池子 in pq.池子s)
            //    {
            //        for (int i = 0; i < pq池子.Works.Count; i++)
            //        {
            //            WorkStation workStation = pq池子.Works[i];
            //            workStation.Name = $"{pq池子.Name}_槽位{i + 1}";
            //        }
            //    }
            //}

            //pq.Lis工件s.Clear();

            //return;
            //var  d= ProductLine.Instance.片区s[0].WorkStations[0];
            //d.当前工件 = new 工件() { Name = "测试工件" };
            //return;
            Task.Run(() =>
            {
                //this.
               
                //主通道调度.Car=new Car() { Name=$"主通道调度小车"};
                //ProductLine.Instance.Cars.Add(主通道调度.Car);
                //主通道调度.Car.PassVM= 主通道调度.PassVM;
                //主通道调度.Car.Position = 500;
                //主通道调度.Car.Position = 500;
                //主通道调度.Car.XLength = 600;
                //主通道调度.Car.YLength = 600;
                //主通道调度.Car.ZLength = 600;
                ProductLine.Instance.Select配方 = ProductLine.Instance.配方s[0];
                Task.Factory.StartNew(() =>
                {
                    主通道调度.Deal上料Station(主通道调度.上料WorkStation);
                },TaskCreationOptions.LongRunning);
                Task.Factory.StartNew(() =>
                {
                    主通道调度.Deal下料WorkStation(主通道调度.下料WorkStation);
                }, TaskCreationOptions.LongRunning);
                Task.Factory.StartNew(() =>
                {
                    主通道调度.Deal();
                }, TaskCreationOptions.LongRunning);

                for (int i = 0; i < 4; i++)
                {

                    var pq = ProductLine.Instance.片区s[i];
                    for (int j = 0; j < pq.CarCount; j++)
                    {
                        int groupID = j + 1;
                        Task.Factory.StartNew(() =>
                        {

                            pq.DealCar(groupID);
                        }, TaskCreationOptions.LongRunning);
                    }
                   
                    foreach (var workStation in pq.WorkStations)
                    {
                        Task.Factory.StartNew(() =>
                        {
                            pq.DealWorkStation(workStation);
                        }, TaskCreationOptions.LongRunning);
                    }
                }

               

                
               
                //for (int i = 0; i <1;i++)
                //{
                //    片区 pq = ProductLine.Instance.片区s[i];

                //}
            });
        }

        public void FillCar(Car Car)
        {
            Car.Position = 500;
            Car.XLength = 600;
            Car.YLength = 600;
            Car.ZLength = 600;
            Car.Background = Brushes.Blue;
            Car.FontSize = 140;
            Car.IsUseYOffset=true;
            ProductLine.Instance.Cars.Add(Car);
        }


        public void Add(string name, int time)
        {
            ProductLine.Instance.工位类型s.Add(new 工位类型() { Name = name, Time = time * 60 });
        }

        /// <summary>
        /// 添加秒
        /// </summary>
        /// <param name="name"></param>
        /// <param name="time"></param>
        public void AddSencond(string name, int time)
        {
            ProductLine.Instance.工位类型s.Add(new 工位类型() { Name = name, Time = time  });
        }

    }
}
